<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.enterprise.gsa.crf.*" %>
<%@ page import="javax.jdo.Query" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
	    <style>
			body {
				font-family: arial, sans-serif;
				background-color:#fff;
				font-size: small;
				margin: 24px 8px 8px;
				color:#000;
			}
			#header {
				position: relative;
				height:50px;
				margin:0 0 11px;
			}
			#logo {  /* in header */
				float:left;
				width: 175px;
				top:-5px;
				left:0;
				padding:0;
				margin:0;
				position:absolute;
			}
			#hints {  /* in header */
				margin:5px 0 0 174px;
				padding:1px 0 1px 3px;
			
			}
			h1, h2, h3, h4, h5 {
				font-weight:bold;
				margin-bottom:0;
			}
			h1 {
				font-size:large;
				font-weight:bold;
				border-top: 1px solid;
				background-color: #e5ecf9;
				border-color: #3366CC;
				margin:5px 0 0 174px;
				padding:1px 0 1px 3px;
			}
			h2 {
				font-size:120%;
				margin-top:1.5em;
				border-bottom: 1px solid;
				border-color: #3366CC;
			}
			h3 {
				font-size:110%;
				margin-top:.7em;
				left:0;
				top:.7em;
			}
			form {
				margin:0;
				padding0;
			}
		</style>
	    <title>
	      Google Search Appliance Dashboard
	    </title>
	    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
	    <script type="text/javascript">
	      google.load('visualization', '1', {packages: ['table','columnchart']});
	    </script>
	    <script type="text/javascript">
		    function drawVisualization() {
		        // Create and populate the data table.
		        var data = new google.visualization.DataTable();
		        data.addColumn('string', 'Query');
		        data.addColumn('number', 'Frequency');

		        var dataClicksPerRow = new google.visualization.DataTable;
		        dataClicksPerRow.addColumn('string', 'Row');
		        dataClicksPerRow.addColumn('number', 'Clicks');

		        var dataRequests = new google.visualization.DataTable;
		        
		        dataRequests.addColumn('string', 'Date');
		        dataRequests.addColumn('string', 'Query');
		        dataRequests.addColumn('string', 'Action');
		        dataRequests.addColumn('number', 'Row');
		        dataRequests.addColumn('string', 'Document');
		        
		        <%
		        PersistenceManager pm = PMF.get().getPersistenceManager();
		        String query = "select from " + DBQueryFrequency.class.getName() + " order by frequency desc";

		        List<DBQueryFrequency> records = (List<DBQueryFrequency>) pm.newQuery(query).execute();
		        if (records.isEmpty()) {
		        } else {
		        	out.println("data.addRows(" + records.size() + ");");
		        	int i = 0;
		            for (DBQueryFrequency cr : records) {            
		            	out.println("data.setCell(" + i + ", 0, '" + cr.getQuery() + "');");
		            	out.println("data.setCell(" + i + ", 1, " + cr.getFrequency() + ");");
		            	i++;
		            }
		        }

		        String query2 = "select from " + DBClicksPerRow.class.getName() + " order by row asc";
		        
		        List<DBClicksPerRow> records2 = (List<DBClicksPerRow>) pm.newQuery(query2).execute();
		        if (records2.isEmpty()) {
		        } else {
		        	out.println("dataClicksPerRow.addRows(" + records2.size() + ");");
		        	int i = 0;
		            for (DBClicksPerRow cr : records2) {            
		            	out.println("dataClicksPerRow.setCell(" + i + ", 0, '" + cr.getRow() + "');");
		            	out.println("dataClicksPerRow.setCell(" + i + ", 1, " + cr.getClicks() + ");");
		            	i++;
		            	if (i > 10) break;
		            }
		        }
		        
		        String query3 = "select from " + DBClickRecord.class.getName() + " order by date desc";
	        	Query theQuery = pm.newQuery(query3);
	        	theQuery.setRange(0,15);
		        List<DBClickRecord> records3 = (List<DBClickRecord>) theQuery.execute();
		        if (records3.isEmpty()) {
		        } else {
		        	//out.println("dataRequests.addRows(" + records3.size() + ");");
		        	//int numRecords = (records3.size() < 21) ? records3.size() : 20;
		        	//out.println("dataRequests.addRows(" + numRecords + ");");
		        	int numRecords = records3.size();
		        	out.println("dataRequests.addRows(" + numRecords + ");");
		        	int i = 0;
		            for (DBClickRecord cr : records3) {          
		            	out.println("dataRequests.setCell(" + i + ", 0, '" + cr.getDate() + "');");
		            	out.println("dataRequests.setCell(" + i + ", 1, '" + cr.getQ() + "');");
		            	out.println("dataRequests.setCell(" + i + ", 2, '" + cr.getCt() + "');");
		            	int realRow = cr.getStart() + cr.getR();
		            	out.println("dataRequests.setCell(" + i + ", 3, " + realRow + ");");
		            	String url = cr.getUrl();
		            	if (url.isEmpty())
		            		url = "N/A";
		            	
		            	out.println("dataRequests.setCell(" + i + ", 4, '" + url + "');");
		            	i++;
		            	//if (i == 20) break;
		            }
		        }
		        
		        pm.close();
		        %>
			      
		        // Create and draw the visualization.
		        var visualization = new google.visualization.Table(document.getElementById('table'));  		        
		        var formatter = new google.visualization.TableBarFormat({width: 200});
		        formatter.format(data, 1); // Apply formatter to second column
		        visualization.draw(data, {allowHtml: true, showRowNumber: true});

		        var chart = new google.visualization.ColumnChart(document.getElementById('chart'));
		        chart.draw(dataClicksPerRow, null);

		        var table = new google.visualization.Table(document.getElementById('table2'));
		        table.draw(dataRequests, null);
		      }
		      
		      google.setOnLoadCallback(drawVisualization);
	      </script>
	</head>
	<body style="font-family: Arial;border: 0 none;">
	<div id="header">
		<div id="logo"><a href="http://code.google.com/"><img src="http://code.google.com/images/code_sm.png" border="0" alt="Return to Google Code homepage" /></a></div>
		<h1 id="doc_title">Google Search Appliance Dashboard</h1>
		<table border="0" width="100%" cellpadding="15">
			<tr>
				<td valign="top">
					<h2>Most frequent queries</h2>
					<div id="table"></div>
				</td>
				<td valign="top">
					<h2>Clicks per row</h2>
					<div id="chart" style="width: 500px; height: 300px; is3D: true"></div>
				</td>
			</tr>
		</table>
		<table border="0" width="100%" cellpadding="15">
			<tr>
				<td>
					<h2>Last search requests</h2>
					<div id="table2"></div>
				</td>
			</tr>
		
		</table>
	</div>
	</body>
</html>